import { Inject, Controller, Post, File } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';
import { UploadMiddleware, UploadFileInfo } from '@midwayjs/busboy';
import { TencentService } from '../service/tencent.service';

@Controller('/auth-api/file')
export class FileController {
  @Inject()
  ctx: Context;

  @Inject()
  tencentService: TencentService;

  @Post('/upload-single', { middleware: [UploadMiddleware] })
  async sigleFileUpload(
    @File()
    file: UploadFileInfo,
  ) {
    if (!file) {
      this.ctx.status = 400;
      this.ctx.body = {
        code: 400,
        success: false,
        timestamp: Date.now(),
        data: null,
        message: '文件不能为空'
      };
      return;
    }
    return this.tencentService.uploadFile(file);
  }
}
